package DP;

/**
 * @author 曹见朋
 * @create 2024-06-13-22:26
 */
public class Main337 {
    // TODO 337. 打家劫舍 III
    public static int rob(TreeNode root) {
        int[] result = robSub(root);
        return Math.max(result[0], result[1]);
    }

    private static int[] robSub(TreeNode node) {
        if (node == null) {
            return new int[2];
        }
        int[] left = robSub(node.left);
        int[] right = robSub(node.right);
        int[] res = new int[2];

        // res[0] 是不偷当前节点的最大值
        res[0] = Math.max(left[0], left[1]) + Math.max(right[0], right[1]);
        // res[1] 是偷当前节点的最大值
        res[1] = node.val + left[0] + right[0];

        return res;
    }

    public static void main(String[] args) {

    }
}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}